﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/5/29 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// 时序编辑窗体
    /// </summary>
    public partial class FormTimeLineEdit : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="timeline"></param>
        public FormTimeLineEdit(DeviceTimeLine timeline = null)
        {
            InitializeComponent();
            LoadTimeLine(timeline);
            this.Title = timeline != null ? "编辑时序信息" : "新增时序信息";
        }
        private void LoadTimeLine(DeviceTimeLine timeLine = null)
        {
            string code = string.Empty, name = string.Empty, address = string.Empty;
            if (timeLine != null)
            {
                code = timeLine.TimeLineCode;
                name = timeLine.TimeLineName;
                address = timeLine.TimeLinePLCAddress;
                this.labelID.Tag = timeLine;
            }
            this.textBoxTimeLineCode.Text = code;
            this.textBoxTimeLineName.Text = name;
            this.textBoxTimeLinePLCAddress.Text = address;
            if (!string.IsNullOrWhiteSpace(code))
            {
                //不可以编辑
                this.textBoxTimeLineCode.ReadOnly = true;
                this.textBoxTimeLineCode.Enabled = false;
            }
        }
        private void FormTimeLineEdit_Load(object sender, EventArgs e)
        {
            this.labelStart.Select();
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            string code = this.textBoxTimeLineCode.Text;
            string name = this.textBoxTimeLineName.Text;
            string address = this.textBoxTimeLinePLCAddress.Text;
            DeviceTimeLine timeline = this.labelID.Tag == null ? new DeviceTimeLine() : (DeviceTimeLine)this.labelID.Tag;
            bool isNew = this.labelID.Tag == null ? true : false;
            timeline.TimeLineCode = code;
            timeline.TimeLineName = name;
            timeline.TimeLinePLCAddress = address;
            if (isNew)
            {
                if (timeline.IsValid() && TimeLine.Instance().DeviceTimeLines.Find(v => v.TimeLineCode.Equals(code)) == null)
                {
                    TimeLine.Instance().DeviceTimeLines.Add(timeline);
                    HWMessageBox.Show(this, "新增时序配置成功");
                    this.Close();
                }
                else
                {
                    HWMessageBox.Show(this, "新增时序配置失败");
                }
            }
            else
            {
                if (timeline.IsValid() && TimeLine.Instance().DeviceTimeLines.Find(v => v.TimeLineCode.Equals(code)) != null)
                {
                    var findItem = TimeLine.Instance().DeviceTimeLines.Find(v => v.TimeLineCode.Equals(code));
                    findItem.TimeLineName = name;
                    findItem.TimeLinePLCAddress = address;
                    HWMessageBox.Show(this, "保存时序配置成功");
                    this.Close();
                }
                else
                {
                    HWMessageBox.Show(this, "保存时序配置失败");
                }
            }
        }
        private void buttonMaxSize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
        private void FormTimeLineEdit_Shown(object sender, EventArgs e)
        {
        }
        private void FormTimeLineEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
    }
}
